package com.lfc.flowable.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfc.flowable.domian.AjaxResult;
import com.lfc.flowable.domian.LeaveApply;
import com.lfc.flowable.service.LeaveApplyService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 请假申请控制层
 */
@RestController
@RequestMapping("/leave")
public class LeaveApplyController {

    @Resource
    LeaveApplyService leaveApplyService;

    /**
     * 查询请假申请列表
     *
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/getLeaveList")
    public Page<LeaveApply> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String userId) {
        return leaveApplyService.getLeavePage(pageNum, pageSize, userId);
    }

    /**
     * 任务详情信息
     *
     * @param taskId
     * @return
     */
    @GetMapping("/businessData")
    public AjaxResult getBusinessData(@RequestParam String taskId) {
        LeaveApply leaveApply = leaveApplyService.getBusinessData(taskId);
        return AjaxResult.success(leaveApply);
    }

    /**
     * 发起请求流程（提交请假申请）
     *
     * @param leaveapply
     * @return
     */
    @PostMapping("/addLeaveApply")
    public AjaxResult addLeaveApply(@RequestBody LeaveApply leaveapply) {
        leaveapply.setApplyTime(new Date());
        leaveApplyService.addLeaveApply(leaveapply);
        return AjaxResult.success();
    }

    /**
     * 审批
     *
     * @param taskId
     * @param variables
     * @return
     */
    @PostMapping(value = "/completeTask/{taskId}")
    public AjaxResult completeTask(@PathVariable("taskId") String taskId,
                                   @RequestBody(required = false) Map<String, Object> variables) {
        leaveApplyService.completeTask(taskId, variables);
        return AjaxResult.success();
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        leaveApplyService.deleteLeaveApplyByIds(ids);
        return AjaxResult.success();
    }
}
